package com.gexiaobao.pigeon.ui.fragment.mine.tools.query

import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseFragment
import com.gexiaobao.pigeon.app.ext.init
import com.gexiaobao.pigeon.app.ext.initClose
import com.gexiaobao.pigeon.app.ext.initFooter
import com.gexiaobao.pigeon.app.ext.loadListData
import com.gexiaobao.pigeon.app.ext.loadServiceInit
import com.gexiaobao.pigeon.app.ext.showLoading
import com.gexiaobao.pigeon.databinding.FragmentNameRaceBinding
import com.gexiaobao.pigeon.ui.adapter.SearchRaceByNameListAdapter
import com.gexiaobao.pigeon.viewmodel.MineToolsViwModel
import com.kingja.loadsir.core.LoadService
import me.hgj.jetpackmvvm.ext.nav
import me.hgj.jetpackmvvm.ext.navigateAction

/**
 * @Author      : hxw
 * @Date        : 2023/8/7 14:27
 * @Describe    : 鸽主参加的比赛列表
 */
class FragmentNameRace : BaseFragment<MineToolsViwModel, FragmentNameRaceBinding>() {

    private var title: String = ""
    private val mAdapter: SearchRaceByNameListAdapter by lazy { SearchRaceByNameListAdapter(arrayListOf()) }
    private lateinit var loadsir: LoadService<Any>

    override fun initView(savedInstanceState: Bundle?) {
        title = arguments?.getString("title").toString()
        mDatabind.includeBar.toolbar.initClose(title) {
            nav().navigateUp()
        }

        mDatabind.layoutList.swipeRecyclerView.init(LinearLayoutManager(context), mAdapter).let {
            it.initFooter {
                /** 触发加载更多时请求数据 */
                showLoadingAntiShake()
                mViewModel.searchRaceByName(true, title)
            }
        }

        /** 状态页配置 */
        loadsir = loadServiceInit(mDatabind.layoutList.swipeRefreshLayout) {
            /** 点击重试时触发的操作 */
            loadsir.showLoading()
            mViewModel.searchRaceByName(true, title)
        }

        /** 下拉刷新 */
        mDatabind.layoutList.swipeRefreshLayout.init {
            showLoadingAntiShake()
            mViewModel.searchRaceByName(true, title)
        }

        mAdapter.addChildClickViewIds(R.id.itemRaceByNameDetail)
        mAdapter.setOnItemChildClickListener { _, _, position ->
            nav().navigateAction(R.id.action_to_fragment_race_by_name_detail, Bundle().apply {
                putString("name", title)
                putString("raceName", mAdapter.data[position].raceName)
            })
        }
    }

    override fun lazyLoadData() {
        super.lazyLoadData()
        showLoading(getString(R.string.loading))
        mViewModel.searchRaceByName(true, title)
    }


    override fun createObserver() {
        super.createObserver()
        mViewModel.searchRaceByNameResult.observe(viewLifecycleOwner) {
            dismissLoadingAntiShake()
            loadListData(it, mAdapter, loadsir, mDatabind.layoutList.swipeRecyclerView, mDatabind.layoutList.swipeRefreshLayout)
        }
    }
}